import { createSlice, createAsyncThunk, type PayloadAction } from "@reduxjs/toolkit";
import * as API from '../api/http'
import type { shopTS, typeTS } from "./crmsTS";

type initialStateTS = {
    shoplist: Array<shopTS>,
    typelist: Array<typeTS>,
    activeKey: string
}

let initialState = {
    shoplist: [],//存放商品列表数据
    typelist: [],//存放分类列表数据
    activeKey: '1',//高亮下标1
}


const todosSlice = createSlice({
    name: "todos",
    initialState,
    reducers: { //同步方法

        //获取商品列表数据
        t_getshoplist(state: initialStateTS, action: PayloadAction<Array<shopTS>>) {
            state.shoplist = action.payload
        },
        //获取分类列表数据
        t_gettypelist(state: initialStateTS, action: PayloadAction<Array<typeTS>>) {
            state.typelist = action.payload
        },
        //商品列表添加数据
        t_addshoplist(state: initialStateTS, action: PayloadAction<shopTS>) {
            state.shoplist.push(action.payload)
        },
        //商品列表编辑数据
        t_editshoplist(state: initialStateTS, action: PayloadAction<shopTS>) {
            let i = state.shoplist.findIndex(item => {
                return item.id == action.payload.id
            })
            state.shoplist[i] = action.payload
        },
        //商品列表删除数据
        t_delshoplist(state: initialStateTS, action: PayloadAction<number>) {
            state.shoplist = state.shoplist.filter(item => {
                return item.id !== action.payload
            })
        },
        //分类列表高亮
        t_change(state, action) {
            state.activeKey = action.payload
        },
        //分类列表添加数据
        t_addtypelist(state: initialStateTS, action: PayloadAction<typeTS>) {
            state.typelist.push(action.payload)
        },
        //分类列表删除数据
        t_deltypelist(state: initialStateTS, action: PayloadAction<number>) {
            state.typelist = state.typelist.filter(item => {
                return item.key !== action.payload
            })
        },

    },
});

export const actionfn = todosSlice.actions;
export default todosSlice.reducer;

//异步操作 商品列表数据
export let async_shoplist = createAsyncThunk('async_shoplist', async (_, store) => {
    //调取接口
    let res = await API.http_shoplist()
    //同步执行异步
    store.dispatch(actionfn.t_getshoplist(res))
})

//异步操作 分类列表数据
export let async_typelist = createAsyncThunk('async_typelist', async (_, store) => {
    //调取接口
    let res = await API.http_typelist()
    //同步执行异步
    store.dispatch(actionfn.t_gettypelist(res))
})

//异步操作 商品列表添加数据
export let async_addshoplist = createAsyncThunk('async_addshoplist', async (obj: shopTS, store) => {
    //调取接口
    let res = await API.http_addshoplist(obj)
    //同步执行异步
    store.dispatch(actionfn.t_addshoplist(res))
})
//异步操作 商品列表编辑数据
export let async_editshoplist = createAsyncThunk('async_editshoplist', async (obj: shopTS, store) => {
    //调取接口
    let res = await API.http_editshoplist(obj)
    //同步修改异步
    store.dispatch(actionfn.t_editshoplist(res))
})
//异步操作 商品列表删除数据
export let async_delshoplist = createAsyncThunk('async_delshoplist', async (id: number, store) => {
    //调取接口
    await API.http_delshoplist(id)
    store.dispatch(actionfn.t_delshoplist(id))
})
//异步操作 商品列表搜索数据
export let async_searchshoplist = createAsyncThunk('async_searchshoplist', async (title: string, store) => {
    //调取接口
    let res = await API.http_searchshoplist(title)
    //同步修改异步
    store.dispatch(actionfn.t_getshoplist(res))
})

//异步操作 分类列表添加数据
export let async_addtypelist = createAsyncThunk('async_addtypelist', async (obj: typeTS, store) => {
    //调取接口
    let res = await API.http_addtypelist(obj)
    //同步执行异步
    store.dispatch(actionfn.t_addtypelist(res))
})

//异步操作 商品列表删除数据
export let async_deltypelist = createAsyncThunk('async_deltypelist', async (id: number, store) => {
    //调取接口
    await API.http_deltypelist(id)
    store.dispatch(actionfn.t_deltypelist(id))
})


